//
//  CollectionCell.m
//  LoveCar
//
//  Created by Xue Senhong on 15/5/21.
//  Copyright (c) 2015年 薛森鸿. All rights reserved.
//

#import "CollectionCell.h"
#import "ArticleCell.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "MainNewsViewController.h"
#import "NewsDetViewController.h"

#import "MJRefreshHeaderView.h"
#import "MJRefreshFooterView.h"
#import "MJRefresh.h"
#import <AFNetworkActivityIndicatorManager.h>



@implementation CollectionCell

- (void)dealloc
{
    [_tableView release];
    [_arr release];
    [_dic release];
    [_newArr release];
    [_index release];
    [_art release];
    [super dealloc];
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.arr = [NSMutableArray array];
        self.dic = [NSMutableDictionary dictionary];
        self.newArr = [NSMutableArray array];
        // tableView
        self.tableView = [[UITableView alloc] init];
        [self.tableView setDelegate:self];
        [self.tableView setDataSource:self];
        [self.contentView addSubview:self.tableView];
        [self.tableView registerClass:[ArticleCell class] forCellReuseIdentifier:@"reuse"];
        [self.tableView setScrollsToTop:YES];
        [_tableView release];
        // 开始刷新
        [self setupRefresh];
    }
    return self;
}

- (void)reloadDataForCell
{
    
    [[AFNetworkActivityIndicatorManager sharedManager] setEnabled:YES];
    // 数据源
    NSString *news = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt1-p1-s20-l0.html";
    NSString *test = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt3-p1-s20-l0.html";
    NSString *shop = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt60-p1-s20-l0.html";
    NSString *situation = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c210200-nt2-p1-s20-l0.html";
    NSString *using = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt82-p1-s20-l0.html";
    NSString *tech = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt102-p1-s20-l0.html";
    NSString *culture = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt97-p1-s20-l0.html";
    NSString *change = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt107-p1-s20-l0.html";
    NSString *trip = @"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt100-p1-s20-l0.html";
    NSString *talker = @"http://app.api.autohome.com.cn/autov4.0/news/shuokelist-a2-pm1-v4.0.1-p1-s20.html";
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:news, test, shop, situation, using, tech, culture, change, trip, talker, nil];
    // 判断当前是哪一页
    if ([self.index integerValue] == 2) {
        [NetworkHandler connectNetGETDataWithURLString:arr[0] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 3) {
        [NetworkHandler connectNetGETDataWithURLString:arr[1] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 4) {
        [NetworkHandler connectNetGETDataWithURLString:arr[2] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 5) {
        [NetworkHandler connectNetGETDataWithURLString:arr[3] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 6) {
        [NetworkHandler connectNetGETDataWithURLString:arr[4] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 7) {
        [NetworkHandler connectNetGETDataWithURLString:arr[5] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 8) {
        [NetworkHandler connectNetGETDataWithURLString:arr[6] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 9) {
        [NetworkHandler connectNetGETDataWithURLString:arr[7] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 10) {
        [NetworkHandler connectNetGETDataWithURLString:arr[8] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"newslist"];
            [self.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 12) {
        [NetworkHandler connectNetGETDataWithURLString:arr[9] completion:^(id result) {
            self.arr = [[result objectForKey:@"result"] objectForKey:@"list"];
            [self.tableView reloadData];
        }];
    }
    self.newsPage = 1;
    self.testPage = 1;
    self.shopPage = 1;
    self.situationPage = 1;
    self.usingPage = 1;
    self.techPage = 1;
    self.culturePage = 1;
    self.changePage = 1;
    self.tripPage = 1;
    self.talkerPage = 1;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    [self.tableView setFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [self.tableView addHeaderWithTarget:self action:@selector(headerRereshing)];
    // 自动刷新(一进入程序就下拉刷新)
    [self.tableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [self.tableView addFooterWithTarget:self action:@selector(footerRereshing)];
}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
//    [self.arr removeAllObjects];
    [self reloadDataForCell];

//    // 刷新表格
//    [self.tableView reloadData];
//    
//    // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
    [self.tableView headerEndRefreshing];

}

- (void)footerRereshing
{
    // 数据源
    NSString *news = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt1-p%ld-s20-l0.html", self.newsPage += 1];
    NSString *test = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt3-p%ld-s20-l0.html", self.testPage += 1];
    NSString *shop = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt60-p%ld-s20-l0.html", self.shopPage += 1];
    NSString *situation = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c210200-nt2-p%ld-s20-l0.html", self.situationPage += 1];
    NSString *using = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt82-p%ld-s20-l0.html", self.usingPage += 1];
    NSString *tech = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt102-p%ld-s20-l0.html", self.techPage += 1];
    NSString *culture = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt97-p%ld-s20-l0.html", self.culturePage += 1];
    NSString *change = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt107-p%ld-s20-l0.html", self.changePage += 1];
    NSString *trip = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/newslist-a2-pm1-v4.0.1-c0-nt100-p%ld-s20-l0.html", self.tripPage += 1];
    NSString *talker = [NSString stringWithFormat:@"http://app.api.autohome.com.cn/autov4.0/news/shuokelist-a2-pm1-v4.0.1-p%ld-s20.html", self.talkerPage += 1];
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:news, test, shop, situation, using, tech, culture, change, trip, talker, nil];
    
    __block CollectionCell *collectionCell = self;
    
    if ([self.index integerValue] == 2) {
        [NetworkHandler connectNetGETDataWithURLString:arr[0] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 3) {
        [NetworkHandler connectNetGETDataWithURLString:arr[1] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 4) {
        [NetworkHandler connectNetGETDataWithURLString:arr[2] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 5) {
        [NetworkHandler connectNetGETDataWithURLString:arr[3] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 6) {
        [NetworkHandler connectNetGETDataWithURLString:arr[4] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 7) {
        [NetworkHandler connectNetGETDataWithURLString:arr[5] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 8) {
        [NetworkHandler connectNetGETDataWithURLString:arr[6] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 9) {
        [NetworkHandler connectNetGETDataWithURLString:arr[7] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 10) {
        [NetworkHandler connectNetGETDataWithURLString:arr[8] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"newslist"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
    if ([self.index integerValue] == 12) {
        [NetworkHandler connectNetGETDataWithURLString:arr[9] completion:^(id result) {
            collectionCell.dic = [result objectForKey:@"result"];
            collectionCell.newArr = [collectionCell.dic objectForKey:@"list"];
            [collectionCell.arr addObjectsFromArray:collectionCell.newArr];
            [collectionCell.tableView reloadData];
        }];
    }
        // 刷新表格
        [self.tableView reloadData];
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [self.tableView footerEndRefreshing];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ArticleCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    self.dic = [self.arr objectAtIndex:indexPath.row];
    self.art = [[Article alloc] init];
    // KVC
    [self.art setValuesForKeysWithDictionary:self.dic];
    [cell valueforCellWithArticle:self.art];
    [_art release];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.arr count];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    // 让代理人调用方法
    [self.delegate pushViewControllerWithString:[[self.arr objectAtIndex:indexPath.row] objectForKey:@"id"]];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
